package com.graden.background.service.sysroleuser.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graden.background.repository.sysroleuser.SysRoleUserMapper;
import com.graden.background.repository.sysroleuser.entity.SysRoleUser;
import com.graden.background.service.sysroleuser.ISysRoleUserService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *  用户角色关联服务实现类
 * </p>
 *
 * @author zjf
 * @since 2019-03-21
 */
@Service
public class SysRoleUserServiceImpl extends ServiceImpl<SysRoleUserMapper, SysRoleUser> implements ISysRoleUserService {

	/**
	 * 插入用户角色关联
	 */
	@Override
	public void saveRoleUser(int userId, Integer[] roleIds) {
		if(!Objects.isNull(roleIds)){
			for (Integer roleId : roleIds){
				if(!Objects.isNull(roleId)){
					SysRoleUser sysRoleUser = new SysRoleUser();
					sysRoleUser.setId(0);
					sysRoleUser.setUserId(userId);
					sysRoleUser.setRoleId(roleId);
					sysRoleUser.setCreateTime(LocalDateTime.now());
					sysRoleUser.setUpdateTime(LocalDateTime.now());
					this.save(sysRoleUser);
				}
			}
		}
	}

	/**
	 * 根据用户id删除关联信息
	 */
	@Override
	public void deleteRoleUserByUserId(int userId) {
		QueryWrapper<SysRoleUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id",userId);
		this.remove(queryWrapper);
	}
}
